<template>

  <el-dialog
    top="20px"
    :title="defaultValue.title"
    :lock-scroll="tableConfig.lockScroll"
    :append-to-body="tableConfig.appendToBody"
    :visible="tableConfig.show"
    @close="tableConfig.show = false"
    :show-close="tableConfig.showClose"
    :destroy-on-close="tableConfig.destroyOnClose"
    width="1100px">

    <el-form label-width="250px" >
      <el-form-item>
        <span>当前准备迁移数据用户：{{getName(this.selectUser)}}</span>
      </el-form-item>


      <el-form-item prop="memberFamilyTree">
            <span slot="label">
              可迁移目标(当前人会作为该目标子级)
            </span>
        <el-select v-model="memberFamilyTreeId" placeholder="请选择父对象" clearable filterable>
          <el-option
            v-for="item in memberFamilyTree"
            :key="item.id"
            :label="getName(item)"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button plain type="warning" @click="migrate" >迁移</el-button>
      <el-button plain type="primary" @click="tableConfig.show = false">关闭</el-button>
    </span>

  </el-dialog>

</template>

<script>

import http from '@/utils/http'
import {checkNull, getFullTime} from "../../utils/utils";

export default {
  components: {},
  data() {
    return {
      // 表格的默认值
      tableConfig: {
        // 关闭时销毁 Dialog 中的元素
        destroyOnClose: true,
        // 弹出框是否打开
        show: false,
        // 是否显示关闭按钮
        showClose: true,
        // 下面2个属性解决打开弹出框后没有在最上面显示的问题，导致看到的内容是一半的，需要先将滚动条移到最上面
        lockScroll: false,
        appendToBody: true,
      },
      // 默认值，进入页面后的值
      defaultValue: {
        // 弹出框标题
        title: '查看',
        // 保存还是修改or查看,取值有 select | save | upd
        saveUpdSelect: 'select'
      },
      // 可选的用户数据
      memberFamilyTree: [],
      // 选择的源用户ID
      memberFamilyTreeId: null,
      // 当前选中的人
      selectUser: null,
    };
  },
  methods: {
    /**
     * 迁移数据用户到另外一个用户下面
     */
    migrate(){
      http.post("/user/migrate", {
        userId: this.selectUser.id,
        sourceId: this.memberFamilyTreeId
      }).then(response => {
        let res = response.data;
        if (res.status === 'error'){
          if (res.code !== 401){
            this.$message.error(res.msg);
          }
          return
        }
        this.memberFamilyTree = res.data;
        // console.log(this.memberFamilyTree)

        this.selectUser = null
        this.memberFamilyTreeId = null
        this.memberFamilyTree = []
        this.$emit('saveUpdMemberFamilyCallback', null)
        this.tableConfig.show = false

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
    /**
     * 获取一个有效的名字
     * @param item 人员数据
     */
    getName(item){
      if (item == null){
        return ''
      }
      let name = item.userName
      if (item.userName === 'UNKNOWN_NAME_DEFAULT' || checkNull(item.userName)){
        name = "未知姓名";
      }
      let nickName = ''
      if (!checkNull(item.nickname)){
        nickName = item.nickname
      }
      return '姓名：[' + name + '] 昵称:[' + nickName + '] 称呼:[' + item.call + "]"
    },
    /**
     * 打开弹出框之前
     * @param selectUser 当前选中的数据
     */
    openMigrateOpt(selectUser){
      this.selectUser = selectUser
      this.getTransfer()
      this.tableConfig.show = true
      // this.$store.commit('navigationListChange', ['家庭管理', '家庭成员', this.defaultValue.title])
    },
    /**
     * 获取可迁移的目标用户
     * 不能是自己的子级，不能是自己
     */
    getTransfer(){
      // console.log(selectUser)
      http.post("/user/getTransfer", {
        userId: this.selectUser.id
      }).then(response => {
        let res = response.data;
        if (res.status === 'error'){
          if (res.code !== 401){
            this.$message.error(res.msg);
          }
          return
        }
        this.memberFamilyTree = res.data;
        // console.log(this.memberFamilyTree)

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
    /**
     * 是保存还是修改家庭成员
     */
    toSaveUpd(){
      if (this.defaultValue.saveUpdSelect === 'save'){
        // this.savMemberFamily()

      } else if (this.defaultValue.saveUpdSelect === 'upd'){
        // this.updMemberFamily()

      } else {

      }
    },
  },
  mounted() {

  },
}
</script>

<style scoped>

</style>
